/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.lom.examples;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ieee.ltsc.lom.LOM;
import org.ieee.ltsc.lom.impl.LOMImpl;
import org.ontspace.MetadataRepository;
import org.ontspace.owl.util.MetadataRepositoryConfigException;
import org.ontspace.lom.LOMQueryManager;
import org.ontspace.lom.owl.LOMQueryResultImpl;
import org.ontspace.owl.MetadataRepositoryFactory;
import org.ontspace.owl.MetadataRepositoryImpl;
import org.ontspace.owl.util.OntologyNotInRepositoryException;

/**
 * Example for a LOM Query
 */
public class ExampleLOMQuerying {

    public static void main(String args[]) throws MetadataRepositoryConfigException,
            OntologyNotInRepositoryException {


        HashMap params =
                new HashMap();
        params.put(MetadataRepository.STORAGE_URI_PARAM,
                "jdbc:mysql://localhost/lom-ospace");
        params.put(MetadataRepository.USER_NAME_PARAM,
                "root");
        params.put(MetadataRepository.PWD_PARAM,
                "password");
        params.put(MetadataRepositoryImpl.DB_DRIVER_PARAM,
                "com.mysql.jdbc.Driver");
        params.put(MetadataRepositoryImpl.DB_TYPE_PARAM,
                "MySQL");
        params.put(MetadataRepositoryImpl.DC_SCHEMA_PATH_PARAM,
                "C:/mydirectory/cvs/ont-space/etc/lom-ont-space.owl");
        try {

            params.put(MetadataRepository.PUBLIC_REP_URI_PARAM,
                    new URI("http://o-space.org/rep"));
        } catch (URISyntaxException ex) {
            Logger.getLogger(ExampleLOMQuerying.class.getName()).log(Level.SEVERE,
                    null, ex);
        }



        MetadataRepository rep =
                MetadataRepositoryFactory.openMetadataRepository(params);

        // Get session
        //Session sess = rep.getAnonymousSession();

        HashMap paramsLOM =
                new HashMap();
        paramsLOM.put("uri1", "http://www.cc.uah.es/ie/ont/lom-ospace");
        //paramsLOM.put("session",sess);

        //Get the queryManager
        LOMQueryManager qm =
                (LOMQueryManager) rep.getQueryManager
                    ("org.ontspace.lom.owl.LOMQueryManagerImpl",paramsLOM);



        LOMQueryResultImpl result =
                new LOMQueryResultImpl();


        //set query params
        LOM lom =
                generateExampleLOMRecord();
        List cm =
                new ArrayList();
        //SET PROPERTIES
        //0- Structure
        //1- Aggregation Level
        //2- SemanticDensity
        //3- Difficulty
        //4- Interactivity Type
        //5- Interactivity Level

        cm.add(0, "Exactly");
        cm.add(1, "Exactly");
        cm.add(2, "Exactly");
        cm.add(3, "Exactly");
        cm.add(4, "Exactly");
        cm.add(5, "Exactly");



        //launch query
        result =
                qm.queryLO(lom, cm);

        //show results
        result.getResults();


    }

    private static LOM generateExampleLOMRecord() {

        LOMImpl lom =
                new LOMImpl();

//        //1. GENERAL CATEGORY            
        LOMImpl.General general =
                lom.newGeneral();

//        //1.1 IDENTIFIER
//
//        LOMImpl.General.Identifier identifier = general.newIdentifier(0);
//        LOMImpl.General.Identifier.CatalogImpl catalogI =
//                identifier.newCatalog();
//        catalogI.setString("URL");
//        LOMImpl.General.Identifier.EntryImpl entryI = identifier.newEntry();
//        entryI.setString
//            ("http://www.agroecologia.net/socios/articulos%20socios/Alternativas_BM.pdf");

        //1.2 TITLE

        LOMImpl.General.Title title =
                general.newTitle();
        LOMImpl.General.Title.StringImpl stringTitle =
                title.newString(0);
        stringTitle.setString("Agricultura ecológica. Sus fundamentos");
        LOMImpl.General.Title.String.LanguageImpl langTitle =
                stringTitle.newLanguage();
        langTitle.setValue("es");



//        //1.3 LANGUAGE
//
//        LOMImpl.General.Language language = general.newLanguage(0); 
//        language.setString("en"); 
//
//        //1.4 DESCRIPTION 
//
//        LOMImpl.General.Description description = general.newDescription(0);
//        LOMImpl.General.Description.StringImpl stringDescription = 
//                description.newString(0);
//        stringDescription.setString("Se ha considerado que antes de 1998 España era el cuarto país del mundo, después de EE.UU., Japón e Italia, en consumo de bromuro de metilo (BrMe) con un total de 4.191 t, que se aplican como fumigante a unas 8.988 ha de suelo cultivado, principalmente fresón (33%), pimiento (29%) y cucurbitáceas (9%). Después de la reducción del 25 % en 1998 acordada por la EU, el consumo actual de BrMe en España se puede valorar en 3.143 t. Se ha demostrado que entre las alternativas químicas, la combinación de 1,3-dicloropropeno (1,3-D) más cloropicrina y dosis reducidas de metam sodio cuando se aplican con solarización resultan tan eficaces como el BrMe. Entre las alternativas no químicas destaca la biofumigación y solarización, así como cultivos sin suelo, rotación de cultivos, variedades resistentes e injertos. Las alternativas tienen un coste inferior y una eficacia similar al BrMe, no presentando dificultad en su aplicación.");
//        LOMImpl.General.Description.String.LanguageImpl langDescription = 
//                stringDescription.newLanguage();
//        langDescription.setValue("es");
//
//        //1.5 KEYWORD
//
//        LOMImpl.General.Keyword keyword = general.newKeyword(0);
//        LOMImpl.General.Description.StringImpl stringKeyword = 
//                keyword.newString(0);
//        stringKeyword.setString("Biofumigación");
//        LOMImpl.General.Description.String.LanguageImpl langKeyword = 
//                stringKeyword.newLanguage();
//        langKeyword.setValue("es");
//
//        //1.6 COVERAGE LO
//
//         lom.newGeneral().newCoverage(0).newString(0).
//                 setString("CoverageInstance1");
//         
//          LOMImpl.General.Coverage coverage = general.newCoverage(0);
//        LOMImpl.General.Coverage.StringImpl stringcoverage = 
//                coverage.newString(0);
//        stringcoverage.setString("CoverageInstance1");
//        LOMImpl.General.Coverage.String.LanguageImpl langCoverage = 
//                stringcoverage.newLanguage();
//        langCoverage.setValue("es");
//
//        // 1.7 STRUCTURE
//
//        LOMImpl.General.Structure structure = general.newStructure();
//
//        LOMImpl.General.Structure.ValueImpl valueStructure = 
//                structure.newValue();
//        valueStructure.setString("atomic");
//
//        //1.8  AGGREGATION LEVEL
//
//        lom.newGeneral().newAggregationLevel().newSource().setString("LOMv1.0");
//        lom.newGeneral().newAggregationLevel().newValue().setString("1");
//
//        //2. LIFECYCLE CATEGORY            
//        LOMImpl.LifeCycle lifeCycle = lom.newLifeCycle();
//
//        //2.1 VERSION
//
//        LOMImpl.LifeCycle.Version version = lifeCycle.newVersion();
//        LOMImpl.LifeCycle.Version.StringImpl stringVersion = 
//                version.newString(0);
//        stringVersion.setString("LOM 1.0");
//        LOMImpl.LifeCycle.Version.String.LanguageImpl langVersion = 
//                stringVersion.newLanguage();
//        langVersion.setValue("en");
//
//        //2.2 STATUS
//        lifeCycle.newStatus().newValue().setString("final");


//
//        // CONTRIBUTE LIFECYCLE
//
//        LOMImpl.LifeCycle.Contribute contribute = lifeCycle.newContribute(0);
//        LOMImpl.LifeCycle.Contribute.ContributeElementRole contributeRole = 
//                contribute.newRole();
//
//        // META METADATA
//
//        LOMImpl.MetaMetadata metaMetadata = lom.newMetaMetadata();
//
//        LOMImpl.MetaMetadata.Identifier identifierMetadata = 
//                metaMetadata.newIdentifier(0);
//        LOMImpl.MetaMetadata.Identifier.CatalogImpl catalogIMM = 
//                identifierMetadata.newCatalog();
//        catalogIMM.setString ("Catalog Identifier MetaMetadata");
//        LOMImpl.MetaMetadata.Identifier.EntryImpl entryIMM = 
//                identifierMetadata.newEntry();
//        entryIMM.setString ("Entry Identifier Metadata"); 
//
//        LOMImpl.MetaMetadata.MetadataSchema metadataSchema = 
//                metaMetadata.newMetadataSchema(0);
//        //LOMImpl.MetaMetadata.MetadataSchema.
//
//        // TECHNICAL
//        LOMImpl.Technical technical = lom.newTechnical();
//
//        // FORMAT MIME
//
//        LOMImpl.Technical.Format formatMime = technical.newFormat(0);
//
//        formatMime.setString("Format mime");
//
//        // SIZE
//
//        LOMImpl.Technical.Size sizeTechnical = technical.newSize();        
//        sizeTechnical.setString("225786");

//
//        // LOCATION
//
//        LOMImpl.Technical.Location locationTechnical = technical.newLocation(0);
//        locationTechnical.setString("http://www.agroecologia.net/socios/articulos%20socios/Alternativas_BM.pdf");


        //5. EDUCATIONAL
//        LOMImpl.Educational educational = lom.newEducational(0);   

        //5.1 INTERACTIVITY TYPE
//        educational.newInteractivityType().newValue().setString("expositive");
        //

//        LOMImpl.Educational.TypicalAgeRange typicalAgeRange = 
//                educational.newTypicalAgeRange(0);
//        LOMImpl.Educational.TypicalAgeRange.StringImpl stringTypicalAgeRange = 
//                typicalAgeRange.newString(0);
//        stringTypicalAgeRange.setString("1-10"); 
//        LOMImpl.Educational.TypicalAgeRange.String.LanguageImpl 
//                languageTypicalAgeRange = stringTypicalAgeRange.newLanguage();
//        languageTypicalAgeRange.setValue("es");
//
//        // REQUIREMENT
//        // INSTALLATION REMARKS
//        // OTHER PLATFORM REQUIREMETS
//         LOMImpl.Technical.OtherPlatformRequirements other = technical.newOtherPlatformRequirements();        
//         LOMImpl.Technical.OtherPlatformRequirements.StringImpl stringOther = 
//                 other.newString(0);
//         stringOther.setString("Adobe reader");
//         LOMImpl.Technical.OtherPlatformRequirements.String.LanguageImpl 
//                languageOther = stringOther.newLanguage();
//         languageOther.setValue("en");


//        // TECHNICAL DURATION

        // 6.RIGHTS
        // 6.1   COST
        //------------------------------------------------------------------
        //9.CLASSIFICATION
        LOMImpl.Classification classification =
                lom.newClassification(0);
//                  
//          //9.1 purpose
        LOMImpl.Classification.Purpose purposeClassification =
                classification.newPurpose();
        purposeClassification.newValue().setString("discipline");
//       
//                        
//        //9.2 taxon path----
//        LOMImpl.Classification.TaxonPath taxonPathClassification =
//                classification.newTaxonPath(0);
////        
////        //9.2.1 source----
//         LOMImpl.Classification.TaxonPath.Source sourceTPC =
//                 taxonPathClassification.newSource();
//         
//         LOMImpl.Classification.TaxonPath.Source.StringImpl stringsourceClas=
//                sourceTPC.newString(0);
//        stringsourceClas.setString("Agrovoc");
//              
//        LOMImpl.Classification.TaxonPath.Source.String.LanguageImpl langSourceClass =
//                stringsourceClas.newLanguage();
//        langSourceClass.setValue("en");
//       
//         //9.2.2 taxon---
//        LOMImpl.Classification.TaxonPath.Taxon taxonTPC =
//                 taxonPathClassification.newTaxon(0);
//        
//        //9.2.2.1 id----
//         LOMImpl.Classification.TaxonPath.Taxon.Id idTTPC =                 
//                 taxonTPC.newId();
//         idTTPC.setString("7203");
//       
//         //9.2.2.2 entry---
//        LOMImpl.Classification.TaxonPath.Taxon.Entry entryTTPC =
//                taxonTPC.newEntry();
//       LOMImpl.Classification.TaxonPath.Taxon.Entry.StringImpl stringEntry=
//                entryTTPC.newString(0);
//        stringEntry.setString("Soil treatment");
//        
//        LOMImpl.Classification.TaxonPath.Taxon.Entry.String.LanguageImpl langEntryClass=
//                stringEntry.newLanguage();
//        langEntryClass.setValue("en");
//        
//         //9.3.Description---
//        LOMImpl.Classification.Description descriptionClassification =
//                classification.newDescription();
//        LOMImpl.Classification.Description.StringImpl stringDesClass=
//                descriptionClassification.newString(0);
//        stringDesClass.setString("Compuesto orgánico halógeno");
//        
//        LOMImpl.Classification.Description.String.LanguageImpl langDesClass=
//                stringDesClass.newLanguage();
//        langDesClass.setValue("es");
//               

        //9.4 keyword
//        LOMImpl.Classification.Keyword keywordClassification =
//                classification.newKeyword(0);
//        LOMImpl.Classification.Keyword.StringImpl stringKeyClas=
//                keywordClassification.newString(0);
//        stringKeyClas.setString("Methyl bromide");
//              
//        LOMImpl.Classification.Keyword.String.LanguageImpl langKeyClass =
//                stringKeyClas.newLanguage();
//        langKeyClass.setValue("en");





        return lom;
    }
}
